﻿using Microsoft.Owin;
using System;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace OAuthServerApi
{
    public class ExceptionMiddleware : OwinMiddleware
    {
        public ExceptionMiddleware(OwinMiddleware next) : base(next) { }

        public override async Task Invoke(IOwinContext context)
        {
            try
            {
                await Next.Invoke(context);
            }
            catch (Exception ex)
            {
                context.Response.ContentType = "application/json;charset=utf8";
                await context.Response.WriteAsync(JsonConvert.SerializeObject(new { error = "exception:"+ ex.Message }));
            }
        }
    }
}